package com.ham.mud;

import com.ham.mud.rooms.RoomService;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.*;

/**
 * Created by hlucas on Jun 16, 2011 at 11:26:51 AM
 */
public class Runner {
    public static Runner STATIC_INSTANCE = null;

    int zoneRegenTick = 0;

    public static void main(String[] args) {
        Runner runner = new Runner();
        STATIC_INSTANCE = runner;
        try {
            runner.start();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    List<ServerConnection> connections = new ArrayList<ServerConnection>();

    public List<ServerConnection> getConnections() {
        return connections;
    }

    public void removeConnection(ServerConnection connection) {
        connections.remove(connection);
    }

    public void addConnection(ServerConnection connection) {
        connections.add(connection);
    }

    private void start() throws IOException {
        ServerSocket listener = new ServerSocket(9999);
        Timer timer = new Timer();
        timer.scheduleAtFixedRate(new TimerTask() {
            @Override
            public void run() {
                preTick();
                tick();
                postTick();
            }
        }, 1000, 1000);
        
        while(true) {
            Socket newSocket = listener.accept();
            ServerConnection serverConnection = new ServerConnection(newSocket);
            Thread t = new Thread(serverConnection);
            t.start();
        }
    }

    private void preTick() {
        Iterator<ServerConnection> it = connections.iterator();
        while(it.hasNext()) {
            ServerConnection next = it.next();
            next.preTick();
        }
    }

    private void tick() {
        Iterator<ServerConnection> it = connections.iterator();
        while(it.hasNext()) {
            ServerConnection next = it.next();
            next.tick();
        }
    }

    private void postTick() {
        boolean didZoneRegen = false;
        zoneRegenTick++;
        if(zoneRegenTick == 90) {
            zoneRegenTick = 0;
            didZoneRegen = zoneRegen();
        }

        Iterator<ServerConnection> it = connections.iterator();
        while(it.hasNext()) {
            ServerConnection next = it.next();
            next.postTick();
            if(didZoneRegen) {
                next.printSolo(Colors.NEW_LINE + getZoneRegenString());
            }
        }
    }

    //todo: make it zone dependent
    private String getZoneRegenString() {
        int rand1 = new Random().nextInt(10);
        switch(rand1) {
            case 0:
                return "You hear the bees buzzing.";
            case 1:
                return "You hear the wolves howl.";
            case 2:
                return "You feel a slight breeze.";
            case 3:
                return "You smell the forest.";
            case 4:
                return "A cloud passes in front of the sun.";
            case 5:
                return "A dandelion seed floats by in the breeze.";
            case 6:
                return "The forest resumes its natural din.";
            case 7:
                return "A grasshopper hops by.";
            default:
                return "You hear the birds chirping.";
        }
    }

    private boolean zoneRegen() {
        return RoomService.clearCache();
    }
}
